package notes.java.sjms23.t14_flyweight;
// 享元模式
// 享元工厂

import java.util.HashMap;
import java.util.Map;

public class Library {
	
	// 书列表
	Map<String, Book> books = new HashMap<>();
	
	// 借书
	public Book getBook(String name) {
		Book book = null;
		// 图书馆有书，直接借出
		if(books.containsKey(name)) {
			book = books.get(name);
		} else {
			// 没有书，调入新书上架
			book = new ConcreteBook(name);
			books.put(name, book);
			System.out.println("图书馆上架：" + name);
		}
		return book;
	}
	
	// 所有书
	public void books() {
		System.out.println("-----------------所有书----------------");
		for(String name : books.keySet()) {
			System.out.print(name + "\t");
		}
	}

}
